/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cics_analyze.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/19 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include <cics/config.h>
#include <cics/cics_core.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <cics/cics_gateway.h>
#include <cics/cics_packet.h>
#include "../test_cics_comm.h"

extern void __cics_add_error_nums(L7session_cics *p_st_sess);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 测试HANDSHAKE请求
 */
static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_cics_create_ct(&p_st_conn, TEST_CICS_SRC_IP, TEST_CICS_DST_IP, TEST_CICS_SRC_PORT, TEST_CICS_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "47617465007010000000000500000000000000000000000442415345000000001600027a680002434e0000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_CICS_SRC_IP, TEST_CICS_DST_IP, TEST_CICS_SRC_PORT, TEST_CICS_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_cics_private *p_st_session_private = (L7session_cics_private *) frct_get_session_private(p_st_session);

    CU_ASSERT_EQUAL(cics_packet_unfragment(p_st_session_private->p_st_sess, p_st_packet, p_st_session_private->p_st_cics_packet), APM_CICS_CHECK_FRAGMENT_SUCCESS);

    CU_ASSERT_EQUAL(cics_gateway(p_st_conn, p_st_session_private->p_st_sess, p_st_session_private->p_st_cics_packet, &st_hash_report), FR_SUCC);

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_cics_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 测试HANDSHAKE响应
 */
static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_cics_create_ct(&p_st_conn, TEST_CICS_SRC_IP, TEST_CICS_DST_IP, TEST_CICS_SRC_PORT, TEST_CICS_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "4761746500701000000000050000000000000000000000044241534500000000340002656e000255530000001e4f533a2041495820417263683a207070632056657273696f6e3a20352e3300000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_CICS_SRC_IP, TEST_CICS_DST_IP, TEST_CICS_SRC_PORT, TEST_CICS_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_cics_private *p_st_session_private = (L7session_cics_private *) frct_get_session_private(p_st_session);

    CU_ASSERT_EQUAL(cics_packet_unfragment(p_st_session_private->p_st_sess, p_st_packet, p_st_session_private->p_st_cics_packet), APM_CICS_CHECK_FRAGMENT_SUCCESS);

    CU_ASSERT_EQUAL(cics_gateway(p_st_conn, p_st_session_private->p_st_sess, p_st_session_private->p_st_cics_packet, &st_hash_report), FR_SUCC);

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_cics_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_cics_gateway(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cics_gateway()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
